<?php
namespace Front\Controller;
use Common\Controller\AgentPublicController;

class PayController extends AgentPublicController {
    
    public function _initialize() {
        parent::_initialize();    
    }
    
    public function getwxconfig(){
        $conffile = SITE_PATH . "conf/pay/spay/pcconfig.php";
        if (file_exists($conffile)) {
            $spayconf = include $conffile;
        } else {
            $spayconf = array();
        }
        
//        $this->spayversion = $spayconf["version"]; // 汇付宝商户号
//        $this->spayurl = $spayconf["url"]; // 汇付宝签名
//        $this->spaymchId = $spayconf["mchId"]; // 汇付宝签名
//        $this->spaykey = $spayconf["key"]; // 汇付宝签名
        
        return $spayconf;
    }
    
    public function wxnotify() {
//        $hs_debug_obj=new \HuoShu\Debug("/debug/charge_debug");
//        $hs_debug_obj->log("notify_start");
        
        $config=$this->getwxconfig();
        
//        $hs_debug_obj->log("get config data");
        
        
        import("Vendor/wftpay/Utils");
		import("Vendor/wftpay/class/ClientResponseHandler");
	
		$resHandler = new \ClientResponseHandler();
	
		$xml = file_get_contents('php://input');
	
		$resHandler->setContent($xml);
		$resHandler->setKey($config['key']);
		//判断签名结果
		$signResult = $resHandler->isTenpaySign();
        
//        $hs_debug_obj->log($signResult.'start db manipulation');
	
		if($signResult){
			if($resHandler->getParameter('status') == 0 && $resHandler->getParameter('result_code') == 0){
				//更改订单状态
				$out_trade_no = $resHandler->getParameter('out_trade_no'); //自己生成的订单状态
				$total_fee = $resHandler->getParameter('total_fee') / 100;
				//将订单状态写入数据表中
                
//                $hs_debug_obj->log("write order record $out_trade_no $total_fee ");
                
				$this->charge_balance_after($out_trade_no, $total_fee*100);
				echo 'success';
				exit;
			}else{
				echo 'failure1';
				exit();
			}
		}else{
			echo 'failure2';
		}
    }
    
    public function charge_balance_after($orderid,$real_amount){

        $model=M('ptb_agentcharge');
        $pre_data=$model->where(array("order_id"=>$orderid))->find();

        //如果订单的状态已经是成功的，就不要再重复更新了
        if($pre_data['status']=='2'){
            return;
        }
        
        //如果发现实际交易金额跟记录的金额不同，就不进行后续的操作了
        //用分为单位比较两者的金额，所以传递过来的real_amount单位但是分
        if($real_amount!=$pre_data['money']*100){
            return;
        }
        
        //订单状态标记为成功，更新时间
        $model->where(array("order_id"=>$orderid))->setField("status","2");
        $model->where(array("order_id"=>$orderid))->setField("update_time",time());
        
        
        $agent_id=$model->where(array("order_id"=>$orderid))->getField("agent_id");
        
        
        //这个订单成功了，渠道用户的平台币余额就要增加
        $add_value=$real_amount/100;
        $hs_pb_obj=new \HuoShu\PtbBalance();
        $hs_pb_obj->Inc($agent_id, $add_value);
        
    }
    
}

